<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 最大N个数与最小N个数的和（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个数组&#xff0c;编写一个函数来计算它的最大N个数与最小N个数的和。你需要对数组进行去重。</p> 
<p><strong>说明&#xff1a;</strong></p> 
<ul><li>数组中数字范围[0, 1000]</li><li>最大N个数与最小N个数不能有重叠&#xff0c;如有<strong><em>重叠&#xff0c;输入非法</em></strong>返回-1</li><li>输入非法返回-1</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<ul><li>第一行输入M&#xff0c; M标识数组大小</li><li>第二行输入M个数&#xff0c;标识数组内容</li><li>第三行输入N&#xff0c;N表达需要计算的最大、最小N个数</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出最大N个数与最小N个数的和</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td> <p>5<br /> 95 88 83 64 100<br /> 2</p> </td></tr><tr><td>输出</td><td>342</td></tr><tr><td>说明</td><td>最大2个数[100,95],最小2个数[83,64], 输出为342。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td> <p>5<br /> 3 2 3 4 2<br /> 2</p> </td></tr><tr><td>输出</td><td>-1</td></tr><tr><td>说明</td><td>最大2个数[4,3],最小2个数[3,2], 有重叠输出为-1。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的逻辑题。主要应该是考察数组常用方法的使用&#xff0c;以及数组去重如何实现。</p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int m &#61; sc.nextInt();

    int[] arr &#61; new int[m];
    for (int i &#61; 0; i &lt; m; i&#43;&#43;) {
      arr[i] &#61; sc.nextInt();
    }

    int n &#61; sc.nextInt();

    System.out.println(getResult(m, arr, n));
  }

  public static int getResult(int m, int[] arr, int n) {
    HashSet&lt;Integer&gt; set &#61; new HashSet&lt;&gt;();

    for (int val : arr) {
      if (val &lt; 0 || val &gt; 1000) return -1;
      set.add(val);
    }

    if (set.size() &lt; n * 2) return -1;

    Integer[] distinct_arr &#61; set.toArray(new Integer[0]);

    Arrays.sort(distinct_arr, (a, b) -&gt; a - b);

    int l &#61; 0;
    int r &#61; distinct_arr.length - 1;
    int ans &#61; 0;

    while (n &gt; 0) {
      ans &#43;&#61; distinct_arr[l] &#43; distinct_arr[r];
      l&#43;&#43;;
      r--;
      n--;
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 3) {
    const m &#61; lines[0] - 0;
    const arr &#61; lines[1].split(&#34; &#34;).map((ele) &#61;&gt; Number(ele));
    const n &#61; lines[2] - 0;
    console.log(getResult(m, arr, n));
    lines.length &#61; 0;
  }
});

function getResult(m, arr, n) {
  const set &#61; new Set();

  for (let val of arr) {
    if (val &lt; 0 || val &gt; 1000) return -1;
    set.add(val);
  }

  if (set.size &lt; n * 2) {
    return -1;
  }

  arr &#61; [...set].sort((a, b) &#61;&gt; a - b);

  let l &#61; 0;
  let r &#61; arr.length - 1;
  let ans &#61; 0;

  while (n &gt; 0) {
    ans &#43;&#61; arr[l] &#43; arr[r];
    l&#43;&#43;;
    r--;
    n--;
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
m &#61; int(input())
arr &#61; list(map(int, input().split()))
n &#61; int(input())


# 算法入口
def getResult(m, arr, n):
    # 你需要对数组进行去重
    arr &#61; list(set(arr))

    # 最大N个数与最小N个数不能有重叠&#xff0c;如有重叠&#xff0c;输入非法返回-1
    if len(arr) &lt; n * 2:
        return -1

    arr.sort()

    # 数组中数字范围[0, 1000]
    if arr[0] &lt; 0 or arr[-1] &gt; 1000:
        return -1

    # 最大N个数与最小N个数的和
    return sum(arr[:n]) &#43; sum(arr[-n:])


# 算法调用
print(getResult(m, arr, n))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>